﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Windows.Forms;

namespace AvmReleaseTool
{
    /// <summary>
    /// 接口类： IAvmRleaseInfo
    /// </summary>
    public interface IAvmReleaseInfoDialog
    {
        //设置目标文档中的GitCommit信息
        void SetReleaseCommitInfo(string info);
        //设置目前文档中的释放日期信息
        void SetReleaseDateInfo(DateTime info);
        //设置目标文档中的Svn信息
        void SetReleaseSvnInfo(string info);

        //设置完需要的信息表之后，进行更新窗口
        void Update();
    }

    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            //AddRightMenu();
            MessageBox.Show("AvmReleaseTool 软件释放开发工具\nAuthor: dota2_black\n请叫我雷锋");
            //初始化程序，在excel中布置插件环境
            AddRightMenu();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {

        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        private void GetXlsxData()
        {

        }

        //对excel右键选定的cell 添加可选功能
        private void AddRightMenu()
        {
            Excel.Application excelApp = this.Application;
            //获取excel的右键单击excel的菜单
            Microsoft.Office.Core.CommandBar mzBar = excelApp.CommandBars["cell"];
            Microsoft.Office.Core.CommandBarControls bars = mzBar.Controls;

            //创建自定义菜单项
            excelButton = bars.Add(
                Microsoft.Office.Core.MsoControlType.msoControlButton,
                missing,
                missing,
                missing,
                true);
            if (excelButton != null)
            {
                excelButton.Caption = "RepoRelease工具";
                excelButton.Tag = "repo2excel";
                (excelButton as Microsoft.Office.Core.CommandBarButton).Click += new Office._CommandBarButtonEvents_ClickEventHandler(OnButton_ExcelReleaseTool);
            }

            repoButton = bars.Add(
                Microsoft.Office.Core.MsoControlType.msoControlButton,
                missing,
                missing,
                missing,
                true);

            if (repoButton != null)
            {
                repoButton.Caption = "ExcelRelease工具";
                repoButton.Tag = "execl2repo";
                (repoButton as Microsoft.Office.Core.CommandBarButton).Click += new Office._CommandBarButtonEvents_ClickEventHandler(OnButton_RepoReleaseTool);
            }
        }

        private void OnButton_ExcelReleaseTool(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Excel.Range range = this.Application.ActiveCell as Excel.Range;
            //新建excel release窗口
            IAvmReleaseInfoDialog excel_form = new ExcelReleaseInfoForm();

            //判断点击的是日期单元格
            try
            {
                string data = range.Value2.ToString();  //获取cell内容
                if (!String.IsNullOrEmpty(data))
                {
                    DateTime release_time;
                    release_time = DateTime.FromOADate(double.Parse(data));
                    excel_form.SetReleaseDateInfo(release_time);
                }
            }
            catch
            {
                MessageBox.Show("不是一个有效的释放节点，请重新选择");
                return;
            }

            //获取选中单元格的最后一行
            Excel.Worksheet sheet = this.Application.ActiveSheet;
            //获取文件名的列是否含有glsdk_rootfs.tar.gz文件,用于确定glsdk_ofilm架构的释放
            for (int index = 0; index < range.MergeArea.Rows.Count; index++)
            {
                int row = range.MergeArea.Row + index;
                string fileName = sheet.Cells[row, 3].Text;
                if (fileName == "glsdk_rootfs.tar.gz")
                {
                    excel_form.SetReleaseCommitInfo(sheet.Cells[row, 5].Text);
                    excel_form.SetReleaseSvnInfo(sheet.Cells[range.MergeArea.Row, 7].Text);
                    break;
                }
            }  
            //string str = "j6_arm_app SHA:\ncb99e2a512bad417986080b012af3fa10cafde01\n\nalgo SHA:\n2fabdc29db4b659a2ff2b10f1879447b2265c382\n\nj6_arm_common SHA:\n52eba188d1f523a964cf5974a1f4cc6b0945a7f2\n\ngraphic_engine SHA:\nf8de605f4a4af22991c9259382e5056034d741eb\n";
            //excel_form.SetReleaseCommitInfo(str);
            excel_form.Update();
        }

        private void OnButton_RepoReleaseTool(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            RepoReleaseInfoForm repo_form = new RepoReleaseInfoForm();
            repo_form.Show();
        }

        Microsoft.Office.Core.CommandBarControl excelButton;
        Microsoft.Office.Core.CommandBarControl repoButton;
        #endregion
    }
}
